<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html" id="parent">
    <p:commandButton value="Create new item" oncomplete="PF('createNewDiag').show();"/>
    <p:commandButton value="Edit item" oncomplete="PF('editNewDiag').show();"/>
    <p:commandButton value="Edit Row" oncomplete="PF('editChildDiag').show();" update="@(.editChildPanel)"/>
    <p:commandButton value="Add row to item" oncomplete="PF('viewer').show();" update="itemTitle"/>
    <p:commandButton value="Print" type="button" icon="ui-icon-print" rendered="#{login.current.department.name eq 'Admin'}">
        <p:printer target="resTable" />
    </p:commandButton>
    <p:dataTable  id="resTable" var="res" value="#{reportsBean.getResources(reportType)}" 
                  paginator="true" rows="100" 
                  paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                  rowsPerPageTemplate="3,25,50,100,250,500"
                  filteredValue="#{reportsBean.filteredResource}" emptyMessage="No Items Entered"
                  > 
        <f:facet name="header">  
            #{reportType} Data

        </f:facet> 
        <p:columnGroup type="header">  
            <p:row>  
                <p:column rowspan="2" headerText="Item" />  
                <p:column colspan="6" headerText="Item Data" />  
            </p:row>  

            <p:row>  
                <p:column headerText="Rate" />  
                <p:column headerText="Loss rate" />  

                <p:column headerText="Quantity" />  
                <p:column headerText="Price" />  
                <p:column headerText="Unit Total Price" /> 
                <p:column headerText="Notes" />  
            </p:row>  
        </p:columnGroup> 
        <p:subTable var="stats" value="#{res.tenderItemsDataList}" id="subTable1" >  
            <f:facet name="header"> 
                #{res.title}(#{res.code})

            </f:facet>                                                            
            <p:column>  
                #{stats.title}  
            </p:column>  
            <p:column>  
                #{stats.rate}  
            </p:column>  

            <p:column>  
                #{stats.lossRate}%
            </p:column>  

            <p:column>  
                #{stats.quantity}  
            </p:column>  
            <p:column>  
                #{stats.price}  
            </p:column>
            <p:column>  
                #{stats.unitTotal}  
            </p:column>
            <p:column>  
                #{stats.notes}  
            </p:column>
            <p:columnGroup type="footer">  
                <p:row>  
                    <p:column footerText="Totals: " style="text-align:right"/>  
                    <p:column footerText="#{res.totalValue}" />  

                </p:row> 
                <p:row>  
                    <p:column footerText="Expences " style="text-align:right"/>  
                    <p:column footerText="#{res.expences}%" />  

                </p:row> 
                <p:row>  
                    <p:column footerText="total Price " style="text-align:right"/>  
                    <p:column footerText="#{res.totalPrice}" />  

                </p:row>
                <p:row>  
                    <p:column footerText="Amount " style="text-align:right"/>  
                    <p:column footerText="#{res.amount}" />  

                </p:row> 
                <p:row>  
                    <p:column footerText="final Price " style="text-align:right"/>  
                    <p:column footerText="#{res.finalPrice}" />  

                </p:row> 
            </p:columnGroup>  
        </p:subTable>

    </p:dataTable>

    <p:dialog widgetVar="createNewDiag" header="Create New" dynamic="true" >
        <p:growl  id='itemData' life="3000" showDetail="true"/>
        <p:panelGrid columns="2" id="newItem">

            <f:facet name="header">Item Data</f:facet>
            <p:outputLabel for="title" value="Title"/>
            <p:inputText id="title" value="#{reportsBean.title}" required="true" requiredMessage="Title can't be empty"/>
            <p:outputLabel for="code" value="Code"/>
            <p:inputText id="code" value="#{reportsBean.code}" required="true" requiredMessage="Code can't be empty"/>
            <p:outputLabel for="expences" value="expenses %"/>
            <p:inputText id="expences" value="#{reportsBean.expences}" required="true" requiredMessage="expences can't be empty">
                <f:validateLongRange />
            </p:inputText>
            <p:outputLabel for="amount" value="amount"/>
            <p:inputText id="amount" value="#{reportsBean.amount}" required="true" requiredMessage="expences can't be empty"/>

            <f:facet name="footer">
                <p:commandButton process="newItem" value="Create" update="editItem,resTable,itemData,itemTitle" action="#{reportsBean.save(reportType)}" oncomplete="handleCreateRequest(xhr, status, args)"/>
            </f:facet>
        </p:panelGrid>
        <script type="text/javascript">
            function handleCreateRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('createNewDiag').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('createNewDiag').hide();

                }
            }
        </script>
    </p:dialog>
    <p:dialog widgetVar="editNewDiag" header="Edit" dynamic="true" >
        <p:growl  id='editItemData' life="3000" showDetail="true"/>
        <p:outputPanel  styleClass="editItemPanel">
            <p:panel id="editItem"  style="overflow: auto">
                <p:fragment autoUpdate="true">
                    <p:panelGrid columns="2"  >
                        <p:outputLabel for="itemToEdit" value="Edit Item"/>
                        <p:fragment autoUpdate="true">
                            <h:panelGrid columns="2">
                                <p:selectOneMenu id="itemToEdit" value="#{reportsBean.selectedResource}" required="true" converter="tenderConverter">
                                    <f:selectItems  var="resItem" value="#{reportsBean.getResources(reportType)}"  itemLabel="#{resItem.title}" itemValue="#{resItem}"/>
                                </p:selectOneMenu>
                                <p:commandButton  process="@(#itemToEdit)" value="Load Data" update="@(.editItemPanel)" />
                            </h:panelGrid>
                        </p:fragment>
                        <f:facet name="header">Item Data</f:facet>
                        <p:outputLabel for="titleEdit" value="Title"/>
                        <p:inputText id="titleEdit" value="#{reportsBean.selectedResource.title}" required="true" requiredMessage="Title can't be empty"/>
                        <p:outputLabel for="codeEdit" value="Code"/>
                        <p:inputText id="codeEdit" value="#{reportsBean.selectedResource.code}" required="true" requiredMessage="Code can't be empty"/>
                        <p:outputLabel for="expencesEdit" value="expenses %"/>
                        <p:inputText id="expencesEdit" value="#{reportsBean.selectedResource.expences}" required="true" requiredMessage="expences can't be empty">
                            <f:validateLongRange />
                        </p:inputText>
                        <p:outputLabel for="amountEdit" value="amount"/>
                        <p:inputText id="amountEdit" value="#{reportsBean.selectedResource.amount}" required="true" requiredMessage="expences can't be empty"/>

                        <f:facet name="footer">
                            <p:commandButton process="editItem" value="Edit" update="resTable,editItemData,itemTitle" action="#{reportsBean.updateItem()}" oncomplete="handleEditDataRequest(xhr, status, args)"/>
                        </f:facet>
                    </p:panelGrid>
                </p:fragment>

            </p:panel>
        </p:outputPanel>
        <script type="text/javascript">
            function handleEditDataRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('editNewDiag').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('editNewDiag').hide();

                }
            }
        </script>
    </p:dialog>
    <p:dialog widgetVar="viewer" header="Edit" dynamic="true"  style="width: auto;" >
        <p:outputPanel  styleClass="editPanel">
            <p:growl  id='editData' life="1500" showDetail="true"/>

            <p:panel id="itemTitle"  style="overflow: auto">
                <p:fragment autoUpdate="true">
                    <p:panelGrid columns="2"  >
                        <p:outputLabel for="items" value="Add to"/>
                        <p:selectOneMenu id="items" value="#{reportsBean.selectedResource}" required="true" converter="tenderConverter">
                            <f:selectItems  var="resItem" value="#{reportsBean.getResources(reportType)}"  itemLabel="#{resItem.title}" itemValue="#{resItem}"/>
                        </p:selectOneMenu>

                        <p:outputLabel for="childTitle" value="Title"/>
                        <p:inputText id="childTitle" value="#{reportsBean.newItem.title}" required="true" requiredMessage="Title can't be empty"/>
                        <p:outputLabel for="lossRate" value="Loss Rate"/>
                        <p:inputText id="lossRate" value="#{reportsBean.newItem.lossRate}" required="true" requiredMessage="Loss Rate can't be empty"/>
                        <p:outputLabel for="price" value="Price"/>
                        <p:inputText id="price" value="#{reportsBean.newItem.price}" required="true" requiredMessage="Price can't be empty"/>
                        <p:outputLabel for="quantity" value="Quantity"/>
                        <p:inputText id="quantity" value="#{reportsBean.newItem.quantity}" required="true" requiredMessage="Quantity can't be empty"/>
                        <p:outputLabel for="rate" value="rate"/>
                        <p:inputText id="rate" value="#{reportsBean.newItem.rate}" required="true" requiredMessage="Rate can't be empty"/>
                        <p:outputLabel for="notes" value="Notes"/>
                        <p:inputTextarea id="notes" value="#{reportsBean.newItem.notes}" required="true" requiredMessage="Notes can't be empty"/>
                        <f:facet name="footer">
                            <p:commandButton process="itemTitle" value="Create" update="resTable,@(.editPanel),editData" action="#{reportsBean.saveData}" oncomplete="handleEditRequest(xhr, status, args);"/>
                        </f:facet>

                    </p:panelGrid>
                </p:fragment>

            </p:panel>


        </p:outputPanel>
        <script type="text/javascript">             function handleEditRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('viewer').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('viewer').hide();

                }
            }
        </script>
    </p:dialog>
    <p:dialog widgetVar="editChildDiag" header="Edit" dynamic="true" modal="true"  style="width: auto;" >
        
        <p:outputPanel  styleClass="editChildPanel">
            <p:growl  id='editChildData' life="1500" showDetail="true"/>

            <p:panel id="itemChildTitle"  style="overflow: auto">
                <p:fragment autoUpdate="true">
                    <p:panelGrid columns="2"  >
                        <p:outputLabel for="childitems" value="Row to Edit"/>
                       
                        <p:fragment autoUpdate="true">
                            <h:panelGrid columns="2">

                                <p:selectOneMenu id="childitems" value="#{reportsBean.editableItem}" required="true" converter="tenderItemsDataConverter">
                                    <f:selectItems  var="resItem" value="#{reportsBean.getChildResources(reportType)}"  itemLabel="#{resItem.title}" itemValue="#{resItem}"/>
                                </p:selectOneMenu>

                                <p:commandButton  process="@(#childitems)" value="Load Data" update="@parent:@parent:@parent" />
                            </h:panelGrid>
                        </p:fragment>

                        <p:outputLabel for="childEditTitle" value="Title"/>
                        <p:inputText id="childEditTitle" value="#{reportsBean.editableItem.title}" required="true" requiredMessage="Title can't be empty"/>
                        <p:outputLabel for="lossRateEdit" value="Loss Rate"/>
                        <p:inputText id="lossRateEdit" value="#{reportsBean.editableItem.lossRate}" required="true" requiredMessage="Loss Rate can't be empty"/>
                        <p:outputLabel for="priceEdit" value="Price"/>
                        <p:inputText id="priceEdit" value="#{reportsBean.editableItem.price}" required="true" requiredMessage="Price can't be empty"/>
                        <p:outputLabel for="quantityEdit" value="Quantity"/>
                        <p:inputText id="quantityEdit" value="#{reportsBean.editableItem.quantity}" required="true" requiredMessage="Quantity can't be empty"/>
                        <p:outputLabel for="rateEdit" value="rate"/>
                        <p:inputText id="rateEdit" value="#{reportsBean.editableItem.rate}" required="true" requiredMessage="Rate can't be empty"/>
                        <p:outputLabel for="notesEdit" value="Notes"/>
                        <p:inputTextarea id="notesEdit" value="#{reportsBean.editableItem.notes}" required="true" requiredMessage="Notes can't be empty"/>
                        <f:facet name="footer">
                            <p:commandButton process="itemChildTitle" value="Create" update="resTable,@(.editChildPanel),editChildData" action="#{reportsBean.updateChildItem()}" oncomplete="handleEditRequest(xhr, status, args);"/>
                        </f:facet>

                    </p:panelGrid>
                </p:fragment>

            </p:panel>


        </p:outputPanel>
        <script type="text/javascript">             function handleEditRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('editChildDiag').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('editChildDiag').hide();

                }
            }
        </script>
           
    </p:dialog>
    
</ui:composition>

